<?php 
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller
{


    // 生成验证码
    public function yzm()
    {
        $Verify = new \Think\Verify();
        $Verify->fontSize = 25;
        $Verify->length = 7;
        $Verify->codeSet = '0123456789';
        $Verify->imageW = 290;
        $Verify->imageH = 50;
        $Verify->entry();
    }

    // 执行登录
    public function doLogin()
    {
        // 接收用户名和密码
        $username = I('post.username');
        $password = I('post.password');

        // 验证用户名和密码
        $config = array(
            'username'=>$username,
            'userpass' =>md5($password)
            );
        // 得到用户信息的数组
        $data = M('aduser')->where($config)->find();
        if (!$data) {
            $this->error('用户名或密码错误！！');
            exit;
        }
        // 将用户的信息存入到session中
        session('admin_user',$data);

        // 根据用户的id 得到相应的节点(权限)信息
        // $list = M('node')->field('mname,aname')->where('id in'.M('rolenode')->field('nid')->where("rid in ".M('userrole')->field('rid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->fetchSql()->select();
        $list = M('node')->field('mname,aname')->where('id in'.M('rolenode')->field('nid')->where("rid in ".M('userrole')->field('rid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->select();


        foreach ($list as $key=>$val) {
            // 把控制器名的首字母改为大写
            $foo = ucfirst($val['mname']);
            $list[$key]['mname'] = $foo ;   
        }

        $nodelist = array();
        $nodelist['Index'] = array('index');
        // 遍历重新拼装
        foreach ($list as $v) {
            $nodelist[$v['mname']][] = $v['aname'];
            if($v['aname'] == 'edit'){
                $nodelist[$v['mname']][]='update';
            }
            if($v['aname'] == 'add'){
                $nodelist[$v['mname']][]='doadd';
            }
        }

        // 将权限放置到session中

        $_SESSION['admin_user']['nodelist'] = $nodelist;



        // 跳转到首页
        $this->redirect('Index/index');
    }

    // 退出登录
    public function outLogin()
    {
        // 清空session
        unset($_SESSION['admin_user']);
        // 跳转
        $this->redirect('Login/index');
    }

}